/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.hazards.wind.buildingdesign;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.TextBoxIntegerValueBlurHandler;
import gov.fema.hazus.bdc.client.hazards.wind.WindConstants;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.WindBuildingDesign;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class WindBuildingDesignForm extends AbstractForm implements BuildingUpdate  {

	private Label designCode;
	private Label designYear;
	private Label designWindSpeed;
	private Label windSpeedType;	
	
	// form fields
	private WindDesignCodeListBox formDesignCode = new WindDesignCodeListBox();
	private TextBox formDesignYear = new TextBox();
	private TextBox formDesignWindSpeed = new TextBox();
	private WindDesignSpeedTypeListBox formWindSpeedType = new WindDesignSpeedTypeListBox();
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(4,2);
	
	// entities
	private WindBuildingDesign windBuildingDesign;
	
	// listeners
	private WindBuildingDesignTextBoxKeyListener windBuildingDesignTextBoxKeyListener;
	private WindBuildingDesignListBoxChangeListener windBuildingDesignListBoxChangeListener;
	private TextBoxIntegerValueBlurHandler textBoxIntegerValueBlurHandler;
	
	private WindConstants constants;
	
	public WindBuildingDesignForm(Building building) {
		
		super(building);
	
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (WindConstants)GWT.create(WindConstants.class);
		tabTitle = constants.windBuildingDesignTabTitle();
		
		designCode = new Label(constants.windBuildingDesignDesignCode());
		designYear = new Label(constants.windBuildingDesignDesignYear());
		designWindSpeed = new Label(constants.windBuildingDesignDesignWindSpeed());
		windSpeedType = new Label(constants.windBuildingDesignWindSpeedType());
		
		// add labels to form
		table.setWidget(0, 0, designCode);
		table.setWidget(1, 0, designYear);
		table.setWidget(2, 0, designWindSpeed);
		table.setWidget(3, 0, windSpeedType);
		
		// add fields to table
		windBuildingDesign = building.getWindBuildingDesign();
		windBuildingDesignTextBoxKeyListener = new WindBuildingDesignTextBoxKeyListener(windBuildingDesign);
		windBuildingDesignListBoxChangeListener = new WindBuildingDesignListBoxChangeListener(windBuildingDesign);
		textBoxIntegerValueBlurHandler = new TextBoxIntegerValueBlurHandler();
		
		// listeners
		loadListeners();
		
		// forms
		loadForms();
		
		// add fields to table
		table.setWidget(0, 1, formDesignCode);
		table.setWidget(1, 1, formDesignYear);
		table.setWidget(2, 1, formDesignWindSpeed);
		table.setWidget(3, 1, formWindSpeedType);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);	
		
	}

	public void updateBuilding(Building building) {

		// entities
		setBuilding(building);
		this.windBuildingDesign = building.getWindBuildingDesign();
		
		// listeners
		windBuildingDesignTextBoxKeyListener.updateWindBuildingDesign(windBuildingDesign);
		windBuildingDesignListBoxChangeListener.updateWindBuildingDesign(windBuildingDesign);
		
		// reload forms
		loadForms();
	}

	private void loadForms()
	{
		if( windBuildingDesign != null)
		{
			formDesignCode.setListItem( windBuildingDesign.getDesignCode() );
			formDesignCode.setName(WindBuildingDesign.DESIGNCODE);
			
			String windBuildingDesignYear = "";
			if( windBuildingDesign.getDesignYear() != null )
			{
				windBuildingDesignYear = windBuildingDesign.getDesignYear().toString();
			}
			formDesignYear.setText( windBuildingDesignYear );
			formDesignYear.setName(WindBuildingDesign.DESIGNYEAR);
			
			String windBuildingDesignWindSpeed = "";
			if( windBuildingDesign.getDesignYear() != null )
			{
				windBuildingDesignWindSpeed = windBuildingDesign.getDesignWindSpeed().toString();
			}
			formDesignWindSpeed.setText( windBuildingDesignWindSpeed );
			formDesignWindSpeed.setName(WindBuildingDesign.DESIGNWINDSPEED);
			
			formWindSpeedType.setListItem( windBuildingDesign.getWindSpeedType() );
			formWindSpeedType.setName(WindBuildingDesign.WINDSPEEDTYPE);
		}
		
		if( building != null)
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formDesignCode.setEnabled(false);
	            formDesignYear.setEnabled(false);
	            formDesignWindSpeed.setEnabled(false);
	            formWindSpeedType.setEnabled(false);
	        }
	        else
	        {
	            formDesignCode.setEnabled(true);
	            formDesignYear.setEnabled(true);
	            formDesignWindSpeed.setEnabled(true);
	            formWindSpeedType.setEnabled(true);
	        }
		}
        
	}

	private void loadListeners()
	{
		formDesignCode.addChangeListener(windBuildingDesignListBoxChangeListener);
		
		formDesignYear.addFocusListener(windBuildingDesignTextBoxKeyListener);
		formDesignYear.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formDesignWindSpeed.addFocusListener(windBuildingDesignTextBoxKeyListener);
		formDesignWindSpeed.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formWindSpeedType.addChangeListener(windBuildingDesignListBoxChangeListener);
	}
}
